package com.huicoo.forestmanager.ui.patrol;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseActivity;
import com.huicoo.forestmanager.network.HttpService;
import com.huicoo.forestmanager.network.bean.patrol.AutoinfoBean;
import com.huicoo.forestmanager.network.bean.patrol.ReportrecordinfoBean;
import com.huicoo.forestmanager.util.DisplayUtil;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.toast.ToastUtils;
import com.huicoo.forestmanager.widget.LoadingDialog;
import com.tianditu.android.maps.GeoPoint;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 巡查记录报送
 */
public class InspectionRecordActivity extends BaseActivity {
    //天气
    @BindView(R.id.weather)
    EditText weather;

    //巡山地点
    @BindView(R.id.garrick_site)
    EditText garrick_site;

    //巡护情况
    @BindView(R.id.patrol_situation)
    EditText patrol_situation;

    //备注
    @BindView(R.id.note)
    EditText note;

    //报送
    @BindView(R.id.ubmission)
    TextView ubmission;

    //单选组
    @BindView(R.id.rg)
    RadioGroup rg;

//    //无
//    @BindView(R.id.radio0)
//    RadioButton radio0;
//
//    //已处理
//    @BindView(R.id.radio1)
//    RadioButton radio1;
//
//    //已上报
//    @BindView(R.id.radio2)
//    RadioButton radio2;

    private LoadingDialog loadingDialog;
    private String mRecordID;
    private String mStopPatrolAddress;

    //是否选择
    private boolean mWhetherChoose=false;
    //选中文案
    private String mChineseCase="";

    public static void start(Activity activity, String RecordID, GeoPoint stopPatrolPoint, String stopPatrolAddress) {
        Intent intent = new Intent(activity, InspectionRecordActivity.class);
        intent.putExtra("RecordID", RecordID);
        intent.putExtra("stopPatrolAddress", stopPatrolAddress);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_inspection_record;
    }

    @Override
    protected void initView() {
        mRecordID = getIntent().getStringExtra("RecordID");
        mStopPatrolAddress = getIntent().getStringExtra("stopPatrolAddress");
        //设置巡林地址
        garrick_site.setText(mStopPatrolAddress);


        //初始化监听
        initListener();

        //获取巡查自动填报信息
        getautoinfo(mRecordID);

    }


    private void initListener() {
        //      监听选中的单选框
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

            }
        });
    }


    @OnClick({R.id.ubmission, R.id.back_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ubmission://报送

                int count = rg.getChildCount();
                for(int i = 0 ;i < count;i++){
                    RadioButton rb = (RadioButton)rg.getChildAt(i);
                    if(rb.isChecked()){
                        mWhetherChoose=true;
                        mChineseCase=rb.getText().toString();
                        break;
                    }
                }

                if (!mWhetherChoose){
                    ToastUtils.show("请选择处理结果");
                    return;
                }

                //巡查记录报送
                reportrecordinfo(
                        weather.getText().toString().trim(),//天气
                        garrick_site.getText().toString().trim(),//巡山地点
                        patrol_situation.getText().toString().trim(),//巡护情况
                        mChineseCase,
                        note.getText().toString().trim(),//备注
                        mRecordID
                );
                break;

            case R.id.back_tv://报送
                finish();
                break;
        }
    }

    //巡查记录报送
    public void reportrecordinfo(String weather, String position, String recordsituation, String dealresult, String remark, String recordID) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.show();
        }

        Call<ReportrecordinfoBean> call = HttpService.getInstance().reportrecordinfo(weather, position, recordsituation, dealresult, remark, recordID);
        call.enqueue(new Callback<ReportrecordinfoBean>() {
            @Override
            public void onResponse(Call<ReportrecordinfoBean> call, Response<ReportrecordinfoBean> response) {
                ReportrecordinfoBean body = response.body();
                if(body==null){
                    ToastUtils.show("请求出错,请稍候再试");
                }
                if (TextUtils.equals(response.body().getCode(), "200")) {
                    //数据请求成功
                    LogUtils.e("巡查记录报送 请求成功 : " + JsonUtils.toJson(response.body()));
                    loadingDialog.dismiss();
                    finish();
                }else {
                    ToastUtils.show("请求出错,请稍候再试");
                }
            }

            @Override
            public void onFailure(Call<ReportrecordinfoBean> call, Throwable t) {
                loadingDialog.dismiss();
                Toast.makeText(InspectionRecordActivity.this, "网络失败,请重新请求", Toast.LENGTH_LONG).show();
                LogUtils.v("请求 巡查记录报送 失败");
                t.printStackTrace();
            }
        });
    }


    //获取巡查自动填报信息
    public void getautoinfo(String recordID) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.show();
        }

        Call<AutoinfoBean> call = HttpService.getInstance().getautoinfo(recordID);
        call.enqueue(new Callback<AutoinfoBean>() {
            @Override
            public void onResponse(Call<AutoinfoBean> call, Response<AutoinfoBean> response) {
                AutoinfoBean body = response.body();
                if (body == null) return;

                //设置天气
                weather.setText(body.getData().getWeather());
                //设置巡查简述
                patrol_situation.setText(body.getData().getMsg());
                //设置单选组
                addRadioButton(body);
                //数据请求成功
                LogUtils.e("获取巡查自动填报信息 请求成功 : " + JsonUtils.toJson(response.body()));
                loadingDialog.dismiss();
            }

            @Override
            public void onFailure(Call<AutoinfoBean> call, Throwable t) {
                loadingDialog.dismiss();
                Toast.makeText(InspectionRecordActivity.this, "网络失败,请重新请求", Toast.LENGTH_LONG).show();
                LogUtils.v("请求 获取巡查自动填报信息 失败");
                t.printStackTrace();
            }
        });
    }

    //设置单选组
    public void addRadioButton(AutoinfoBean body) {
        if (body.getData().getDealType() == null || body.getData().getDealType().size() == 0) {

            for (int i = 0; i < 3; i++) {
                RadioButton tempButton = new RadioButton(this);
                tempButton.setBackgroundResource(R.drawable.radiobutton_background);    // 设置RadioButton的背景图片
                Bitmap a = null;
                tempButton.setButtonDrawable(new BitmapDrawable(a ));
                // 设置文字距离按钮四周的距离
                tempButton.setPadding(DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5));
                if (i == 0) {
                    tempButton.setText("无");
                }
                if (i == 1) {
                    tempButton.setText("已处理");
                }
                if (i == 2) {
                    tempButton.setText("已上报");
                }
                tempButton.setTextColor(getResources().getColorStateList(R.color.radiobutton_textcolor));
                rg.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            return;
        }


        for (int i = 0; i < body.getData().getDealType().size(); i++) {
            RadioButton tempButton = new RadioButton(this);
            tempButton.setBackgroundResource(R.drawable.radiobutton_background);    // 设置RadioButton的背景图片
            Bitmap a = null;
            tempButton.setButtonDrawable(new BitmapDrawable(a ));
            // 设置文字距离按钮四周的距离
            tempButton.setPadding(DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5), DisplayUtil.dp2px(this, 5));
            ;                // 设置文字距离按钮四周的距离
            tempButton.setText(body.getData().getDealType().get(i));
            tempButton.setTextColor(getResources().getColorStateList(R.color.radiobutton_textcolor));
            rg.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }


    }

}
